package top.ljlgithub.api.service.impl;

import com.alibaba.druid.util.JdbcUtils;
import com.auth0.jwt.algorithms.Algorithm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ljlgithub.api.bean.Manager;
import top.ljlgithub.api.jwt.JwtUtils;
import top.ljlgithub.api.mapper.ManagerMapper;
import top.ljlgithub.api.service.ManagerService;
import top.ljlgithub.api.utils.RedisUtil;

/**
 * @ClassName
 * @Description TODO
 * @Author L1470
 * @Date 2021/4/25 18:00
 * @Version 1.0
 **/
@Service
public class ManagerServiceImpl implements ManagerService {
    private final String KEY = "top.ljlgithub.api.shiro.realm.JwtRealm.authorizationCache";

    @Autowired
    ManagerMapper managerMapper;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public void register(Manager manager) {
        //1.获取随机Salt
        String salt = JwtUtils.generateSalt(100);
        //2.保存随机盐
        manager.setSalt(salt);
        //3.获取密码
        String password = manager.getPwd();
        //4.生成md5算法
        Md5Hash md5Hash = new Md5Hash(password, salt, JwtUtils.hashIterations);
        //5.根据md5算法生成密文
        String md5 = md5Hash.toHex();
        //6.保存密码
        manager.setPwd(md5);
        //7.保存到数据库
        managerMapper.saveManager(manager);
    }

    @Override
    public Manager login(String username, String password) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        subject.login(token);
        Manager manager = managerMapper.findManagerByUsername(username);
        return manager;
    }

    @Override
    public void addUser(Manager manager) {
        //1.获取长度为100随机Salt
        String salt = JwtUtils.generateSalt(100);
        //2.保存随机盐
        manager.setSalt(salt);
        //3.获取密码
        String password = manager.getPwd();
        //4.生成md5算法
        Md5Hash md5Hash = new Md5Hash(password, salt, JwtUtils.hashIterations);
        //5.根据md5算法生成密文
        String md5 = md5Hash.toHex();
        //6.保存密码
        manager.setPwd(md5);
        managerMapper.addManager(manager);
    }

    @Override
    public void updateUser(int uId, boolean type) {
        managerMapper.updateUser(uId,type);
    }

    @Override
    public Manager findUserById(int id) {
        Manager manager = managerMapper.findUserById(id);
        return manager;
    }

    @Override
    public void deleteUserById(int id) {
        managerMapper.deleteUserById(id);
    }

    @Override
    public void updateUserRole(int id, int rid) {
        managerMapper.updateUserRole(id,rid);
        Manager user = findUserById(id);
        String username = user.getUsername();
        redisUtil.hdel(KEY,username);
    }

    @Override
    public Manager updateUserInfo(Manager manager) {
        managerMapper.updateUserInfo(manager);
        return managerMapper.findUserById(manager.getId());
    }


}
